﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Bingo.SSQ
{
    public partial class ucVerify : UserControl
    {
        #region Attributes

        private SSQ myNumber = new SSQ();
        private SSQ ssqNumber = new SSQ();

        private delegate string myDeleGategetComboBox();
        private delegate void myDeleGatesetResult(bool b, int index, SSQ num);

        #endregion

        #region ucVerify

        public ucVerify()
        {
            InitializeComponent();
        }

        private void ucVerify_Load(object sender, EventArgs e)
        {
            InitComboBox();
            InitDataGridView();
        }

        private void ucVerify_SizeChanged(object sender, EventArgs e)
        {
            this.picClose.Location = new Point(this.Width - this.picClose.Width - 10, 3);
        }

        #endregion

        #region Event

        #region PictureBox

        private void picClose_MouseEnter(object sender, EventArgs e)
        {
            this.picClose.Image = Properties.Resources.close01;
        }

        private void picClose_MouseLeave(object sender, EventArgs e)
        {
            this.picClose.Image = Properties.Resources.close02;
        }

        private void picClose_Click(object sender, EventArgs e)
        {
            if (((Bingo)this.ParentForm).myThread != null && ((Bingo)this.ParentForm).myThread.IsAlive)
            {
                if (MessageBox.Show("正在进行操作，确定要退出吗？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                {
                    return;
                }
                ((Bingo)this.ParentForm).myThread.Abort();
            }
            this.Dispose();            
        }

        private void picVerity_Click(object sender, EventArgs e)
        {
            if (((Bingo)this.ParentForm).myThread != null && ((Bingo)this.ParentForm).myThread.IsAlive)
            {
                MessageBox.Show("正在进行其他操作，请等待操作完成");
                return;
            }

            myNumber.Reds = new List<string>(this.tbReds.Text.Replace(" ", "|").Replace(";", "|").Replace(",", "|").Split('|'));
            myNumber.Blue = this.tbBlue.Text;

            if ((myNumber.Reds.Count < 6) || (this.tbBlue.Text.Length == 0))
            {
                MessageBox.Show("请输入完整的号码");
                return;
            }

            setResult(true, -1, ssqNumber);

            ((Bingo)this.ParentForm).myThread = new Thread(new ThreadStart(ThreadVerify));
            ((Bingo)this.ParentForm).myThread.Start();
        }

        #endregion

        #region ComboBox

        private void cbbPid_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = BingoLib.SSQ.Issue.get(this.cbbPid.SelectedValue.ToString());
            if (dt.Rows.Count == 1)
            {
                this.lblNumber.Text = dt.Rows[0]["red"].ToString().Replace(";", " ");
                this.lblNumber.Text += " - " + dt.Rows[0]["blue"];
            }
        }

        #endregion

        #endregion

        #region Thread

        private void ThreadVerify()
        {
            DataTable dt = null;

            dt = BingoLib.SSQ.Issue.get(getComboBox());
            ssqNumber.Reds = new List<string>(dt.Rows[0]["Red"].ToString().Split(';'));
            ssqNumber.Blue = dt.Rows[0]["Blue"].ToString();

            SSQ Numbers = new SSQ();
            Numbers.Reds = new List<string>();
            foreach (string num in myNumber.Reds)
            {
                if (ssqNumber.Reds.Contains(num)) Numbers.Reds.Add(num);
            }
            if (ssqNumber.Blue == myNumber.Blue) Numbers.Blue = myNumber.Blue;

            string Result = Numbers.Reds.Count.ToString() + "." + (string.IsNullOrEmpty(Numbers.Blue) ? "0" : "1");
            int gid=0;

            dt = BingoLib.SSQ.Setting.getResult("SSQ");
            foreach (DataRow dr in dt.Rows)
            {
                List<string> results = new List<string>(dr["result"].ToString().Split('|'));
                if(results.Contains(Result)) gid=int.Parse(dr["gid"].ToString());
            }

            setResult(true, gid, Numbers);
        }

        #endregion

        #region Method

        private void InitComboBox()
        {
            DataTable dt = BingoLib.SSQ.Issue.getPid(50);
            this.cbbPid.ValueMember = "pid";
            this.cbbPid.DisplayMember = "pid";
            this.cbbPid.DataSource = dt;
            if(this.cbbPid.Items.Count>0) this.cbbPid.SelectedIndex = 0;
        }

        private void InitDataGridView()
        {
            int index = 0;

            index = this.dataGridView1.Rows.Add();
            this.dataGridView1.Rows[index].Cells[0].Value = "一等";
            this.dataGridView1.Rows[index].Cells[1].Value = "中6+1";
            this.dataGridView1.Rows[index].Cells[2].Value = "    当奖池资金低于1亿元时，奖金总额为当期高等奖奖金的70%与奖池中累积的奖金之和，单注奖金按注均分，单注最高限额封顶500万元。                                                      当奖池资金高于1亿元（含）时，奖金总额包括两部分，一部分为当期高等奖奖金的50%与奖池中累积的奖金之和，单注奖金按注均分，单注最高限额封顶500万元；另一部分为当期高等奖奖金的20%，单注奖金按注均分，单注最高限额封顶500万元。";
            this.dataGridView1.Rows[index].Cells[2].Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.dataGridView1.Rows[index].Cells[3].Value = Properties.Resources.p01;
            this.dataGridView1.Rows[index].Height = 70;

            index = this.dataGridView1.Rows.Add();
            this.dataGridView1.Rows[index].Cells[0].Value = "二等";
            this.dataGridView1.Rows[index].Cells[1].Value = "中6+0";
            this.dataGridView1.Rows[index].Cells[2].Value = "当期高等奖奖金的30%";
            this.dataGridView1.Rows[index].Cells[3].Value = Properties.Resources.p02;
            this.dataGridView1.Rows[index].Height = 28;

            index = this.dataGridView1.Rows.Add();
            this.dataGridView1.Rows[index].Cells[0].Value = "三等";
            this.dataGridView1.Rows[index].Cells[1].Value = "中5+1";
            this.dataGridView1.Rows[index].Cells[2].Value = "单注奖金额固定为3000元";
            this.dataGridView1.Rows[index].Cells[3].Value = Properties.Resources.p03;
            this.dataGridView1.Rows[index].Height = 28;

            index = this.dataGridView1.Rows.Add();
            this.dataGridView1.Rows[index].Cells[0].Value = "四等";
            this.dataGridView1.Rows[index].Cells[1].Value = "中5+0\n4+1";
            this.dataGridView1.Rows[index].Cells[2].Value = "单注奖金额固定为200元";
            this.dataGridView1.Rows[index].Cells[3].Value = Properties.Resources.p04;
            this.dataGridView1.Rows[index].Height = 40;

            index = this.dataGridView1.Rows.Add();
            this.dataGridView1.Rows[index].Cells[0].Value = "五等";
            this.dataGridView1.Rows[index].Cells[1].Value = "中4+0\n3+1";
            this.dataGridView1.Rows[index].Cells[2].Value = "单注奖金额固定为10元";
            this.dataGridView1.Rows[index].Cells[3].Value = Properties.Resources.p05;
            this.dataGridView1.Rows[index].Height = 40;

            index = this.dataGridView1.Rows.Add();
            this.dataGridView1.Rows[index].Cells[0].Value = "六等";
            this.dataGridView1.Rows[index].Cells[1].Value = "中2+1\n1+1\n0+1";
            this.dataGridView1.Rows[index].Cells[2].Value = "单注奖金额固定为5元";
            this.dataGridView1.Rows[index].Cells[3].Value = Properties.Resources.p06;
            this.dataGridView1.Rows[index].Height = 50;
        }

        private string getComboBox()
        {
            if (this.InvokeRequired)
            {
                return this.Invoke(new myDeleGategetComboBox(getComboBox)) as string;
            }
            else
            {
                return this.cbbPid.SelectedValue.ToString();
            }
        }

        private void setResult(bool b, int index, SSQ num)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new myDeleGatesetResult(setResult), new object[] { b, index, num });
            }
            else
            {
                if (b)
                {
                    switch (index)
                    {
                        case 0:
                            this.picResult.Image = Properties.Resources.none;
                            this.lblResult.Text = "非常抱歉，您未能中奖";
                            this.lblMyNumber.Text = "";
                            break;
                        case 1:
                            this.picResult.Image = Properties.Resources.Prize01;
                            this.lblResult.Text = "恭喜您中得一等奖，奖金预计500万元";
                            this.lblMyNumber.Text = string.Format("中奖号码：{0} - {1}", string.Join(" ", num.Reds.ToArray()), num.Blue);
                            break;
                        case 2:
                            this.picResult.Image = Properties.Resources.prize02;
                            this.lblResult.Text = "恭喜您中得二等奖，奖金预计30万元";
                            this.lblMyNumber.Text = string.Format("中奖号码：{0} - {1}", string.Join(" ", num.Reds.ToArray()), num.Blue);
                            break;
                        case 3:
                            this.picResult.Image = Properties.Resources.prize03;
                            this.lblResult.Text = "恭喜您中得三等奖，奖金3000元";
                            this.lblMyNumber.Text = string.Format("中奖号码：{0} - {1}", string.Join(" ", num.Reds.ToArray()), num.Blue);
                            break;
                        case 4:
                            this.picResult.Image = Properties.Resources.prize04;
                            this.lblResult.Text = "恭喜您中得四等奖，奖金200元";
                            this.lblMyNumber.Text = string.Format("中奖号码：{0} - {1}", string.Join(" ", num.Reds.ToArray()), num.Blue);
                            break;
                        case 5:
                            this.picResult.Image = Properties.Resources.prize05;
                            this.lblResult.Text = "恭喜您中得五等奖，奖金10元";
                            this.lblMyNumber.Text = string.Format("中奖号码：{0} - {1}", string.Join(" ", num.Reds.ToArray()), num.Blue);
                            break;
                        case 6:
                            this.picResult.Image = Properties.Resources.prize06;
                            this.lblResult.Text = "恭喜您中得六等奖，奖金5元";
                            this.lblMyNumber.Text = string.Format("中奖号码：{0} - {1}", string.Join(" ", num.Reds.ToArray()), num.Blue);
                            break;
                        default:
                            this.picResult.Image = Properties.Resources.loading;
                            this.lblResult.Text = "";
                            this.lblMyNumber.Text = "";
                            break;
                    }
                    this.picResult.Visible = b;
                    this.lblResult.Visible = b;
                    this.lblMyNumber.Visible = b;
                }
            }
        }

        #endregion
    }

    public struct SSQ
    {
        public List<string> Reds;
        public string Blue;
    }
}
